package com.lry.easyExcel.utils;


import java.math.BigDecimal;

/**
 * Created by Administrator on 20.1.11.
 */
public class ByteUtil {

    public static BigDecimal byteFloatToBigDecimal(byte[] bytes){
        if(null==bytes||bytes.length==0)
            return null;
        String hexStr = ByteUtil.bytesToHexString(bytes);
        hexStr = reverseStr(hexStr,true);
        bytes = ByteUtil.hexToByteArray(hexStr);
        return   new BigDecimal(String.valueOf(ByteArrayConveter.getFloat(bytes,0)));
    }

//    public static BigDecimal byteFloatToBigDecimal(byte[] bytes){
//        return   new BigDecimal(String.valueOf(ByteUtil.reverseHexStringToFloat(ByteUtil.bytesToHexString(bytes))));
//    }

    public static String intToHex(int value) {
        String hexString = Integer.toHexString(value);
        if(hexString==null || hexString.length()==0){
            return "00";
        }
        if(hexString.length()==1){
            return "0"+hexString;
        }
        return hexString;
    }


    public static String intToHex4(int value) {
        String hexString = Integer.toHexString(value);
        return reverseStr(getFormatStr(hexString,4),true);
    }

    public static byte[] intToBytes2(int value) {
        byte[] src = new byte[4];
        src[0] = (byte) ((value >> 24) & 0xFF);
        src[1] = (byte) ((value >> 16) & 0xFF);
        src[2] = (byte) ((value >> 8) & 0xFF);
        src[3] = (byte) (value & 0xFF);
        return src;
    }

    public static String bytesToHexString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    public static String toHexString1(byte[] b) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < b.length; ++i) {
            buffer.append(toHexString1(b[i]));
        }
        return buffer.toString();
    }

    public static String toHexString1(byte b) {
        String s = Integer.toHexString(b & 0xFF);
        if (s.length() == 1) {
            return "0" + s;
        } else {
            return s;
        }
    }

    /**
     * 16进制字符串
     *
     * @param b
     * @return
     */
    public static String bytesHexString(byte[] b) {
        String ret = "";
        if (b != null) {
            for (int i = 0; i < b.length; i++) {
                String hex = Integer.toHexString(b[i] & 0xFF);
                if (hex.length() == 1) {
                    hex = "0" + hex + " ";
                } else {
                    hex = "" + hex + " ";
                }
                ret += hex;
            }
        }
        return ret;
    }

    /**
     * hex字符串转byte数组
     * @param inHex 待转换的Hex字符串
     * @return  转换后的byte数组结果
     */
    public static byte[] hexToByteArray(String inHex){
        int hexlen = inHex.length();
        byte[] result;
        if (hexlen % 2 == 1){
            //奇数
            hexlen++;
            result = new byte[(hexlen/2)];
            inHex="0"+inHex;
        }else {
            //偶数
            result = new byte[(hexlen/2)];
        }
        int j=0;
        for (int i = 0; i < hexlen; i+=2){
            result[j]=hexToByte(inHex.substring(i,i+2));
            j++;
        }
        return result;
    }

    /**
     * Hex字符串转byte
     *
     * @param inHex 待转换的Hex字符串
     * @return 转换后的byte
     */
    public static byte hexToByte(String inHex) {
        if(inHex==null){
            return 0;
        }
        return (byte) Integer.parseInt(inHex, 16);
    }

    public static short hexToShort(String inHex) {
        if(inHex==null){
            return 0;
        }
        return (short) Integer.parseInt(inHex, 16);
    }

    public static String bytesHexString(byte b) {
        String ret = "";
        String hex = Integer.toHexString(b & 0xFF);
        if (hex.length() == 1) {
            hex = "0" + hex;
        } else {
            hex = hex ;
        }
        ret += hex;
        return ret;
    }

    public static String convertHexToString(String hex){
        StringBuilder sb = new StringBuilder();
        StringBuilder temp = new StringBuilder();
        for( int i=0; i<hex.length()-1; i+=2 ){
            String output = hex.substring(i, (i + 2));
            int decimal = Integer.parseInt(output, 16);
            sb.append((char)decimal);
            temp.append(decimal);
        }
        return sb.toString();
    }

    public static String clearZero(String str){
        if(str==null || str.length()<=1){
            return str;
        }
        str = str.replaceFirst("^0*", "");
        if(str.length()==0){
            str = "0";
        }
       return str;
    }

    public static String reverseStr(String str,boolean isHex){
        if(str==null){
            return null;
        }
        int len = str.length();
        if(len%2!=0){
            return str;
        }
        StringBuffer sub = new StringBuffer();
        for(int i=len-2;i>=0;i-=2){
            String temp = str.substring(i,i+2);
            if(!isHex){
                temp = hexToIntStr(temp);
            }
            sub.append(temp);
        }
        return sub.toString();
    }

    /**
     *  十六进制转十进制
     */
    public static String hexToIntStr(String n) {
        return getFormatStr(String.valueOf(Integer.parseInt(n,16)),n.length());
    }

    public static String intToHexStr2(int n) {
        return getFormatStr(Integer.toHexString(n),4);
    }

    public static String shortToHex(short n) {
        return getFormatStr(Integer.toHexString(n),4);
    }
    /**
     * 根据length长度补全str，前面填充0
     * @param str
     * @param length
     * @return
     */
    public static String getFormatStr(String str,int length){
        if(str==null){
            str = "";
        }
        if (str.length()>=length) {
            return str;
        }
        int needLength = length-str.length();
        for(int i=0;i<needLength;i++){
            str = "0"+str;
        }
        return str;
    }

    /**
     * @param src   源数组
     * @param begin 源数组要复制的起始位置
     * @param count 要复制的长
     * @return
     */
    public static byte[] subBytes(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        System.arraycopy(src, begin, bs, 0, count);
        return bs;
    }

    public static float reverseHexStringToFloat(String hex) {
        hex = reverseStr(hex,true);
        if(hex==null){
            return 0;
        }
        return Float.intBitsToFloat(Integer.valueOf(hex, 16));
    }

}
